package cn.chinasyq.photoquan.view;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.PopupWindow;

import cn.chinasyq.photoquan.R;

/**
 * Created by 一搏 on 2015/12/10.
 */
public class PlusWindows extends PopupWindow implements View.OnClickListener {

    Activity mContext;
    private int mWidth;
    private int mHeight;
    private int statusBarHeight;

    private Handler mHandler = new Handler();
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private ImageButton ibtn_close;

    public PlusWindows(Activity context) {
        super(context);
        mContext = context;
        init();
    }

    private void init() {
        Rect frame = new Rect();
        mContext.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        statusBarHeight = frame.top;
        DisplayMetrics metrics = new DisplayMetrics();
        mContext.getWindowManager().getDefaultDisplay()
                .getMetrics(metrics);
        mWidth = metrics.widthPixels;
        mHeight = metrics.heightPixels;

        setWidth(mWidth);
        setHeight(mHeight);
    }


    public void showMoreWindow(View anchor) {
        final FrameLayout layout = (FrameLayout) LayoutInflater.from(mContext).inflate(R.layout.layout_plus_windows, null);
        setContentView(layout);

        btn1 = (Button) layout.findViewById(R.id.button1);
        btn2 = (Button) layout.findViewById(R.id.button2);
        btn3 = (Button) layout.findViewById(R.id.button3);
        ibtn_close = (ImageButton) layout.findViewById(R.id.ibtn_close);
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
        ibtn_close.setOnClickListener(this);
        ibtn_close.setRotation(45);
        layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        setBackgroundDrawable(null);
        setOutsideTouchable(true);
        setFocusable(true);
        showAtLocation(anchor, Gravity.BOTTOM, 0, statusBarHeight);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_close:
                dismiss();
                v.setRotation(0);
                break;
            default:
                if (onMenuItemClickListener != null) {
                    onMenuItemClickListener.onClick(v.getId());
                }
        }
    }

    private OnMenuItemClickListener onMenuItemClickListener;

    public void setOnMenuItemClickListener(OnMenuItemClickListener onMenuItemClickListener) {
        this.onMenuItemClickListener = onMenuItemClickListener;
    }

    public interface OnMenuItemClickListener {
        void onClick(int id);
    }

}

